package com.moli.iov.base.remote.impl;

import com.moli.iov.base.constants.DictConstants;
import com.moli.iov.base.model.dto.DictDataQueryByCodeDto;
import com.moli.iov.base.model.dto.req.CompanyVehicleModelReqDto;
import com.moli.iov.base.model.dto.req.QueryCompanyVehicleModelListParamDto;
import com.moli.iov.base.model.dto.res.CompanyVehicleModelResDto;
import com.moli.iov.base.remote.ICompanyVehicleModelRemoteService;
import com.moli.iov.base.service.ICompanyVehicleModelService;
import com.moli.iov.base.service.IDictDataService;
import com.moli.iov.enums.UserTypeEnum;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.JsonUtils;
import com.moli.iov.util.ValidationUtils;
import java.util.Calendar;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 基础设置> 车机型号管理 业务处理
 *
 * @author whl
 */
@Service(group = "${service.group}")
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ICompanyVehicleModelRemoteServiceImpl implements ICompanyVehicleModelRemoteService {

  @Autowired
  private ICompanyVehicleModelService companyVehicleModelService;
  @Autowired
  private IDictDataService iDictDataService;

  @Override
  public RestResponse saveCompanyVehicleModel(CompanyVehicleModelReqDto dto) {
    log.info("系统设置-车机型号管理-添加-入参 CompanyVehicleModelReqDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto,CompanyVehicleModelReqDto.Save.class);
    Date date = Calendar.getInstance().getTime();
    dto.setCreateUser(dto.getSysUserId().toString());
    dto.setGmtCreate(date);
    dto.setGmtModified(date);
    dto.setModifiedUser(dto.getSysUserId().toString());
    dto.setVehicleCommand(JsonUtils.toJson(dto.getVehicleCommandList()));
    if (UserTypeEnum.COMPANY.getFlag() == dto.getSysUserType()) {
      dto.setCompanyId(dto.getSysCompanyId());
      dto.setCompanyName(dto.getSysCompanyName());
    }
    RestResponse restResponse = companyVehicleModelService.saveCompanyVehicleModel(dto);
    log.info("系统设置-车机型号管理-添加-出参 restResponse:{}",JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse<PageExtend<CompanyVehicleModelResDto>> listCompanyVehicleModels(
      QueryCompanyVehicleModelListParamDto queryParam) {
    log.info("系统设置-车机型号管理-列表-入参QueryCompanyVehicleModelListParamDto:{}",JsonUtils.toJson(queryParam));
    ValidationUtils.validateBean(queryParam);
    RestResponse<PageExtend<CompanyVehicleModelResDto>> pageExtendRestResponse = companyVehicleModelService.listCompanyVehicleModels(queryParam);
    log.info("系统设置-车机型号管理-列表-出参pageExtendRestResponse:{}",JsonUtils.toJson(pageExtendRestResponse));
    return pageExtendRestResponse;
  }

  @Override
  public RestResponse updateCompanyVehicleModel(
      CompanyVehicleModelReqDto dto) {
    log.info("系统设置-车机型号管理-更新-入参CompanyVehicleModelReqDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto,CompanyVehicleModelReqDto.Update.class);
    Date date = Calendar.getInstance().getTime();
    dto.setGmtModified(date);
    dto.setModifiedUser(dto.getSysUserId().toString());
    dto.setVehicleCommand(JsonUtils.toJson(dto.getVehicleCommandList()));
    if (UserTypeEnum.COMPANY.getFlag() == dto.getSysUserType()) {
      dto.setCompanyId(dto.getSysCompanyId());
      dto.setCompanyName(dto.getSysCompanyName());
    }
    RestResponse restResponse = companyVehicleModelService.updateCompanyVehicleModel(dto);
    log.info("系统设置-车机型号管理-更新-出参restResponse:{}",JsonUtils.toJson(restResponse));
    return restResponse ;
  }

  @Override
  public RestResponse<CompanyVehicleModelResDto> getCompanyVehicleModel(
      Long companyVehicleModeById) {
    log.info("系统设置-车机型号管理-详情-入参companyVehicleModeById:{}",companyVehicleModeById);
    if (companyVehicleModeById == null) {
      RestResponse.failure("参数为空");
    }
    RestResponse<CompanyVehicleModelResDto> companyVehicleModel = companyVehicleModelService
        .getCompanyVehicleModel(companyVehicleModeById);
    if (companyVehicleModel!=null&&companyVehicleModel.getData()!=null){
      DictDataQueryByCodeDto dto = new DictDataQueryByCodeDto();
      dto.setDictCode(companyVehicleModel.getData().getDeviceModel());
      dto.setDictType(DictConstants.DICT_TYPE_TBOXM);
      RestResponse restResponse = iDictDataService.queryValueByCode(dto);
      if (restResponse!=null&&restResponse.getData()!=null){
        companyVehicleModel.getData().setDeviceModelStr(restResponse.getData().toString());
      }
      DictDataQueryByCodeDto dto1 = new DictDataQueryByCodeDto();
      dto1.setDictCode(companyVehicleModel.getData().getEquipmentType());
      dto1.setDictType(DictConstants.DICT_DEVICE_TYPE);
      RestResponse tpye = iDictDataService.queryValueByCode(dto1);
      if (tpye!=null&&tpye.getData()!=null){
        companyVehicleModel.getData().setEquipmentTypeStr(tpye.getData().toString());
      }
    }
    log.info("系统设置-车机型号管理-详情-出参RestResponse<CompanyVehicleModelResDto>:{}",JsonUtils.toJson(companyVehicleModel));
    return companyVehicleModel;
  }
}
